package com.tudouji.project.bill.controller;

import java.util.ArrayList;
import java.util.List;

import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.bill.domain.Whinform;
import com.tudouji.project.bill.service.IWhinformService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 入库单Controller
 *
 * @author wf
 * @date 2021-01-07
 */
@Slf4j
@RestController
@RequestMapping("/bill/whinform")
public class WhinformController extends BaseController {
    @Autowired
    private IWhinformService whinformService;

    @Autowired
    private ISalespermissionService salespermissionService;

    /**
     * 查询入库单列表
     */
    @PreAuthorize("@ss.hasPermi('bill:whinform:list')")
    @GetMapping("/list")
    public TableDataInfo list(Whinform whinform) {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Whinform> list = new ArrayList<Whinform>();
        if (StringUtils.isNotEmpty(tuserId)) {
            // 查询用户是否有全局权限
            List<Salespermission> permissions = salespermissionService.getPermByScAndFunc(tuserId, "10", "4");
            int flag = 0;
            if (permissions.size() < 1) {// 没有全局权限
                // 渠道
                List<Salespermission> srs = salespermissionService.getPermByScAndFunc(tuserId, "10", "7");
                if (srs.size() > 0) {
                    flag = 1;
                    String[] srGuids = new String[srs.size()];
                    for (int i = 0; i < srs.size(); i++) {
                        srGuids[i] = srs.get(i).getSrguid();
                    }
                    whinform.setSrguids(srGuids);
                    flag = 1;
                }
            } else {
                flag = 1;
            }
            if (flag == 1) {
                startPage();
                list = whinformService.findWhinformList(whinform);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出入库单列表
     */
    @PreAuthorize("@ss.hasPermi('bill:whinform:export')")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Whinform whinform) {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Whinform> list = new ArrayList<Whinform>();
        if (StringUtils.isNotEmpty(tuserId)) {
            // 查询用户是否有全局权限
            List<Salespermission> permissions = salespermissionService.getPermByScAndFunc(tuserId, "10", "4");
            int flag = 0;
            if (permissions.size() < 1) {// 没有全局权限
                // 渠道
                List<Salespermission> srs = salespermissionService.getPermByScAndFunc(tuserId, "10", "7");
                if (srs.size() > 0) {
                    flag = 1;
                    String[] srGuids = new String[srs.size()];
                    for (int i = 0; i < srs.size(); i++) {
                        srGuids[i] = srs.get(i).getSrguid();
                    }
                    whinform.setSrguids(srGuids);
                    flag = 1;
                }
            } else {
                flag = 1;
            }
            if (flag == 1) {
                //startPage();
                list = whinformService.findWhinformList(whinform);
            }
        }
        ExcelUtil<Whinform> util = new ExcelUtil<Whinform>(Whinform.class);
        return util.exportExcel(list, "whinform");
    }

    /**
     * 获取入库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('bill:whinform:query')")
    @GetMapping(value = "/{autoid}")
    public AjaxResult getInfo(@PathVariable("autoid") Long autoid) {
        return AjaxResult.success(whinformService.selectWhinformById(autoid));
    }

    /**
     * 新增入库单
     */
    @PreAuthorize("@ss.hasPermi('bill:whinform:add')")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Whinform whinform) {
        return toAjax(whinformService.insertWhinform(whinform));
    }

    /**
     * 修改入库单
     */
    @PreAuthorize("@ss.hasPermi('bill:whinform:edit')")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Whinform whinform) {
        return toAjax(whinformService.updateWhinform(whinform));
    }

    /**
     * 删除入库单
     */
    @PreAuthorize("@ss.hasPermi('bill:whinform:remove')")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{autoids}")
    public AjaxResult remove(@PathVariable Long[] autoids) {
        return toAjax(whinformService.deleteWhinformByIds(autoids));
    }

    /**
     * 新增入库单
     */
    @Log(title = "生产入库", businessType = BusinessType.INSERT)
    @PostMapping("/produceWhin")
    public AjaxResult produceWhin(@RequestBody Whinform whinform,Long id,String fileIds, String opinion,Long taskid) {
        log.info("----start whin---");
        return whinformService.produceWhin(whinform, id, fileIds,  opinion, taskid);
    }

    //入库单打印
    @GetMapping("/printWhin")
    public AjaxResult printWhin(Long autoid){
        return whinformService.produceWhin(autoid);
    }

    //入库单打印
    @GetMapping("/printWhinHtml")
    public AjaxResult printWhinHtml(Long autoid){
        return whinformService.printWhinHtml(autoid);
    }
}
